﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Agnes.Core;
using Caliburn.Micro;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 编辑文章命令
    /// </summary>
    public class EditArticleCmd
    {
        private static EditArticleCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static EditArticleCmd Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new EditArticleCmd();
                }
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        EditArticleCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("Article Edit Cmd", typeof(EditArticleCmd));
        }

        /// <summary>
        /// 浏览文章的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 浏览文章执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Article selNode = ArticleManager.Instance.SelectedNode as Article;
            if (selNode == null)
            {
                return;
            }

            IDocumentPanel documentPanel = DocumentPanelsHelper.GetDocumentByModel(selNode);
            if (documentPanel != null)
            {
                AgnesServiceManager.AgnesWorkBenchService.FocusedDocumentPanel = documentPanel;
                ((documentPanel.Content as ArticleView).DataContext as ArticleViewModel).IsEditMode = true;
            }
            else
            {
                ArticleViewModel vm = new ArticleViewModel(selNode);
                vm.IsEditMode = true;
                AgnesServiceManager.AgnesWorkBenchService.DocumentPanels.Add(vm);
            }
        }

        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null 
                || (ArticleManager.Instance.SelectedNode is Article) == false)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
